//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "MarketModelPathwiseMultiDeflatedCaplet.h"
using namespace Cephei::QL::Models::Marketmodels::Products::Pathwise;
#include <gen/QL/Models/Marketmodels/EvolutionDescription.h>
#include <gen/QL/Models/Marketmodels/CurveState.h>
#include <gen/QL/Models/Marketmodels/MarketModelPathwiseMultiProduct.h>
using namespace Cephei::QL::Models::Marketmodels;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Models::Marketmodels::Products::Pathwise::CMarketModelPathwiseMultiDeflatedCaplet::CMarketModelPathwiseMultiDeflatedCaplet (Cephei::IVector<Double>^ rateTimes, Cephei::IVector<Double>^ accruals, Cephei::IVector<Double>^ paymentTimes, Double strike) : CMarketModelPathwiseMultiProduct(CMarketModelPathwiseMultiDeflatedCaplet::typeid)
{
    CoVector<Double>^ _CrateTimes;
    CoVector<Double>^ _Caccruals;
    CoVector<Double>^ _CpaymentTimes;
    try
    {
#ifdef HANDLE
        _phMarketModelPathwiseMultiDeflatedCaplet = NULL;
#endif
        CoVector<Double>^ _CrateTimes = safe_cast<CoVector<Double>^> (rateTimes);
        _CrateTimes->Lock();
        INativeVector<Double>^ _NCIrateTimes = _CrateTimes->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCrateTimes = safe_cast<CDoubleVector^>(_NCIrateTimes);
        std::vector<QuantLib::Time>& _rateTimes = static_cast<std::vector<QuantLib::Time>&> (_NCrateTimes->GetReference ());
        CoVector<Double>^ _Caccruals = safe_cast<CoVector<Double>^> (accruals);
        _Caccruals->Lock();
        INativeVector<Double>^ _NCIaccruals = _Caccruals->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCaccruals = safe_cast<CDoubleVector^>(_NCIaccruals);
        std::vector<QuantLib::Real>& _accruals = static_cast<std::vector<QuantLib::Real>&> (_NCaccruals->GetReference ());
        CoVector<Double>^ _CpaymentTimes = safe_cast<CoVector<Double>^> (paymentTimes);
        _CpaymentTimes->Lock();
        INativeVector<Double>^ _NCIpaymentTimes = _CpaymentTimes->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCpaymentTimes = safe_cast<CDoubleVector^>(_NCIpaymentTimes);
        std::vector<QuantLib::Time>& _paymentTimes = static_cast<std::vector<QuantLib::Time>&> (_NCpaymentTimes->GetReference ());
        QuantLib::Rate _strike = (QuantLib::Rate)ValueHelper::Convert (strike);
        _ppMarketModelPathwiseMultiDeflatedCaplet = new boost::shared_ptr<QuantLib::MarketModelPathwiseMultiDeflatedCaplet> (new QuantLib::MarketModelPathwiseMultiDeflatedCaplet ( _rateTimes,  _accruals,  _paymentTimes,  _strike ));
        SetMarketModelPathwiseMultiProduct (boost::dynamic_pointer_cast<QuantLib::MarketModelPathwiseMultiProduct> (*_ppMarketModelPathwiseMultiDeflatedCaplet));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_CrateTimes != nullptr) _CrateTimes->Unlock();
        if (_Caccruals != nullptr) _Caccruals->Unlock();
        if (_CpaymentTimes != nullptr) _CpaymentTimes->Unlock();
    }
}
Cephei::QL::Models::Marketmodels::Products::Pathwise::CMarketModelPathwiseMultiDeflatedCaplet::CMarketModelPathwiseMultiDeflatedCaplet (boost::shared_ptr<QuantLib::MarketModelPathwiseMultiDeflatedCaplet>& childNative, Object^ owner) : CMarketModelPathwiseMultiProduct(CMarketModelPathwiseMultiDeflatedCaplet::typeid)
{
#ifdef HANDLE
	_phMarketModelPathwiseMultiDeflatedCaplet = NULL;
#endif
	_ppMarketModelPathwiseMultiDeflatedCaplet = &childNative;
    _ppMarketModelPathwiseMultiProduct = new boost::shared_ptr<QuantLib::MarketModelPathwiseMultiProduct> (boost::dynamic_pointer_cast<QuantLib::MarketModelPathwiseMultiProduct> (*_ppMarketModelPathwiseMultiDeflatedCaplet));
}
Cephei::QL::Models::Marketmodels::Products::Pathwise::CMarketModelPathwiseMultiDeflatedCaplet::CMarketModelPathwiseMultiDeflatedCaplet (QuantLib::MarketModelPathwiseMultiDeflatedCaplet& childNative, Object^ owner) : CMarketModelPathwiseMultiProduct(CMarketModelPathwiseMultiDeflatedCaplet::typeid)
{
#ifdef HANDLE
	_phMarketModelPathwiseMultiDeflatedCaplet = NULL;
#endif
	_ppMarketModelPathwiseMultiDeflatedCaplet = new boost::shared_ptr<QuantLib::MarketModelPathwiseMultiDeflatedCaplet> (&childNative);
    _ppMarketModelPathwiseMultiProduct = new boost::shared_ptr<QuantLib::MarketModelPathwiseMultiProduct> (boost::dynamic_pointer_cast<QuantLib::MarketModelPathwiseMultiProduct> (*_ppMarketModelPathwiseMultiDeflatedCaplet));
    _MarketModelPathwiseMultiDeflatedCapletOwner = owner;
    _MarketModelPathwiseMultiProductOwner = owner;
}

Cephei::QL::Models::Marketmodels::Products::Pathwise::CMarketModelPathwiseMultiDeflatedCaplet::CMarketModelPathwiseMultiDeflatedCaplet (CMarketModelPathwiseMultiDeflatedCaplet^ copy) : CMarketModelPathwiseMultiProduct(CMarketModelPathwiseMultiDeflatedCaplet::typeid)
{
#ifdef HANDLE
	_phMarketModelPathwiseMultiDeflatedCaplet = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppMarketModelPathwiseMultiDeflatedCaplet = new boost::shared_ptr<QuantLib::MarketModelPathwiseMultiDeflatedCaplet> (copy->GetShared());
        _ppMarketModelPathwiseMultiProduct = new boost::shared_ptr<QuantLib::MarketModelPathwiseMultiProduct> (boost::dynamic_pointer_cast<QuantLib::MarketModelPathwiseMultiProduct> (*_ppMarketModelPathwiseMultiDeflatedCaplet));
    }
}
Cephei::QL::Models::Marketmodels::Products::Pathwise::CMarketModelPathwiseMultiDeflatedCaplet::CMarketModelPathwiseMultiDeflatedCaplet (System::Type^ t) : CMarketModelPathwiseMultiProduct(CMarketModelPathwiseMultiDeflatedCaplet::typeid)
{
#ifdef HANDLE
	_phMarketModelPathwiseMultiDeflatedCaplet = NULL;
#endif
	if (!t->IsSubclassOf(CMarketModelPathwiseMultiDeflatedCaplet::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Models::Marketmodels::Products::Pathwise::CMarketModelPathwiseMultiDeflatedCaplet::CMarketModelPathwiseMultiDeflatedCaplet (QuantLib::Handle<QuantLib::MarketModelPathwiseMultiDeflatedCaplet>& childNative, Object^ owner)  : CMarketModelPathwiseMultiProduct(CMarketModelPathwiseMultiDeflatedCaplet::typeid)
{
	_phMarketModelPathwiseMultiDeflatedCaplet = &childNative;
	_ppMarketModelPathwiseMultiDeflatedCaplet = &static_cast<boost::shared_ptr<QuantLib::MarketModelPathwiseMultiDeflatedCaplet>>(childNative.currentLink());
    _ppMarketModelPathwiseMultiProduct = new boost::shared_ptr<QuantLib::MarketModelPathwiseMultiProduct> (boost::dynamic_pointer_cast<QuantLib::MarketModelPathwiseMultiProduct> (*_ppMarketModelPathwiseMultiDeflatedCaplet));
    _MarketModelPathwiseMultiDeflatedCapletOwner = owner;
}
Cephei::QL::Models::Marketmodels::Products::Pathwise::CMarketModelPathwiseMultiDeflatedCaplet::CMarketModelPathwiseMultiDeflatedCaplet (QuantLib::Handle<QuantLib::MarketModelPathwiseMultiDeflatedCaplet> childNative)  : CMarketModelPathwiseMultiProduct(CMarketModelPathwiseMultiDeflatedCaplet::typeid)
{
	_phMarketModelPathwiseMultiDeflatedCaplet = &childNative;
	_ppMarketModelPathwiseMultiDeflatedCaplet = &static_cast<boost::shared_ptr<QuantLib::MarketModelPathwiseMultiDeflatedCaplet>>(childNative.currentLink());
    _ppMarketModelPathwiseMultiProduct = new boost::shared_ptr<QuantLib::MarketModelPathwiseMultiProduct> (boost::dynamic_pointer_cast<QuantLib::MarketModelPathwiseMultiProduct> (*_ppMarketModelPathwiseMultiDeflatedCaplet));
}
#endif
#ifdef STRUCT
Cephei::QL::Models::Marketmodels::Products::Pathwise::CMarketModelPathwiseMultiDeflatedCaplet::CMarketModelPathwiseMultiDeflatedCaplet (QuantLib::MarketModelPathwiseMultiDeflatedCaplet childNative)  : CMarketModelPathwiseMultiProduct(CMarketModelPathwiseMultiDeflatedCaplet::typeid)
{
#ifdef HANDLE
	_phMarketModelPathwiseMultiDeflatedCaplet = NULL;
#endif
	_ppMarketModelPathwiseMultiDeflatedCaplet = new boost::shared_ptr<QuantLib::MarketModelPathwiseMultiDeflatedCaplet> (new QuantLib::MarketModelPathwiseMultiDeflatedCaplet (childNative));
    _ppMarketModelPathwiseMultiProduct = new boost::shared_ptr<QuantLib::MarketModelPathwiseMultiProduct> (boost::dynamic_pointer_cast<QuantLib::MarketModelPathwiseMultiProduct> (*_ppMarketModelPathwiseMultiDeflatedCaplet));
}
#endif

Cephei::QL::Models::Marketmodels::Products::Pathwise::CMarketModelPathwiseMultiDeflatedCaplet::~CMarketModelPathwiseMultiDeflatedCaplet ()
{
    if (_ppMarketModelPathwiseMultiDeflatedCaplet != NULL)
    {
	    delete _ppMarketModelPathwiseMultiDeflatedCaplet;
        _ppMarketModelPathwiseMultiDeflatedCaplet = NULL;
    }
}
Cephei::QL::Models::Marketmodels::Products::Pathwise::CMarketModelPathwiseMultiDeflatedCaplet::!CMarketModelPathwiseMultiDeflatedCaplet ()
{
    if (_ppMarketModelPathwiseMultiDeflatedCaplet != NULL)
    {
	    delete _ppMarketModelPathwiseMultiDeflatedCaplet;
    }
}
QuantLib::MarketModelPathwiseMultiDeflatedCaplet& Cephei::QL::Models::Marketmodels::Products::Pathwise::CMarketModelPathwiseMultiDeflatedCaplet::GetReference ()
{
    if (_ppMarketModelPathwiseMultiDeflatedCaplet == NULL) throw gcnew NativeNullException ();
	return **_ppMarketModelPathwiseMultiDeflatedCaplet;
}
boost::shared_ptr<QuantLib::MarketModelPathwiseMultiDeflatedCaplet>& Cephei::QL::Models::Marketmodels::Products::Pathwise::CMarketModelPathwiseMultiDeflatedCaplet::GetShared ()
{
    if (_ppMarketModelPathwiseMultiDeflatedCaplet == NULL) throw gcnew NativeNullException ();
	return *_ppMarketModelPathwiseMultiDeflatedCaplet;
}
QuantLib::MarketModelPathwiseMultiDeflatedCaplet* Cephei::QL::Models::Marketmodels::Products::Pathwise::CMarketModelPathwiseMultiDeflatedCaplet::GetPointer ()
{
    if (_ppMarketModelPathwiseMultiDeflatedCaplet == NULL) throw gcnew NativeNullException ();
	return &**_ppMarketModelPathwiseMultiDeflatedCaplet;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::MarketModelPathwiseMultiDeflatedCaplet>& Cephei::QL::Models::Marketmodels::Products::Pathwise::CMarketModelPathwiseMultiDeflatedCaplet::GetHandle ()
{
	if (_phMarketModelPathwiseMultiDeflatedCaplet == NULL)
	{
		_phMarketModelPathwiseMultiDeflatedCaplet = new Handle<QuantLib::MarketModelPathwiseMultiDeflatedCaplet> (*_ppMarketModelPathwiseMultiDeflatedCaplet);
	}
	return *_phMarketModelPathwiseMultiDeflatedCaplet;
}
#endif
bool Cephei::QL::Models::Marketmodels::Products::Pathwise::CMarketModelPathwiseMultiDeflatedCaplet::HasNative () 
{
	return (_ppMarketModelPathwiseMultiDeflatedCaplet != NULL);
}

Boolean Cephei::QL::Models::Marketmodels::Products::Pathwise::CMarketModelPathwiseMultiDeflatedCaplet::AlreadyDeflated::get ()
{
    try
    {
    	bool _rv = (bool)(*_ppMarketModelPathwiseMultiDeflatedCaplet)->alreadyDeflated ( );   
        Boolean _nrv = (Boolean)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Models::Marketmodels::IEvolutionDescription^ Cephei::QL::Models::Marketmodels::Products::Pathwise::CMarketModelPathwiseMultiDeflatedCaplet::Evolution::get ()
{
    try
    {
    	QuantLib::EvolutionDescription& _rv = (QuantLib::EvolutionDescription&)(*_ppMarketModelPathwiseMultiDeflatedCaplet)->evolution ( );   
        Cephei::QL::Models::Marketmodels::CEvolutionDescription^ _nrv = gcnew Cephei::QL::Models::Marketmodels::CEvolutionDescription (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
UInt64 Cephei::QL::Models::Marketmodels::Products::Pathwise::CMarketModelPathwiseMultiDeflatedCaplet::MaxNumberOfCashFlowsPerProductPerStep::get ()
{
    try
    {
    	QuantLib::Size _rv = (QuantLib::Size)(*_ppMarketModelPathwiseMultiDeflatedCaplet)->maxNumberOfCashFlowsPerProductPerStep ( );   
        UInt64 _nrv = (UInt64)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
UInt64 Cephei::QL::Models::Marketmodels::Products::Pathwise::CMarketModelPathwiseMultiDeflatedCaplet::NumberOfProducts::get ()
{
    try
    {
    	QuantLib::Size _rv = (QuantLib::Size)(*_ppMarketModelPathwiseMultiDeflatedCaplet)->numberOfProducts ( );   
        UInt64 _nrv = (UInt64)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::IVector<Double>^ Cephei::QL::Models::Marketmodels::Products::Pathwise::CMarketModelPathwiseMultiDeflatedCaplet::PossibleCashFlowTimes::get ()
{
    try
    {
    	std::vector<QuantLib::Time> _rv = (std::vector<QuantLib::Time>)(*_ppMarketModelPathwiseMultiDeflatedCaplet)->possibleCashFlowTimes ( );   
        Cephei::IVector<Double>^ _nrv = gcnew CoVector<Double> (gcnew CDoubleVector (_rv));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Models::Marketmodels::Products::Pathwise::IMarketModelPathwiseMultiDeflatedCaplet^ Cephei::QL::Models::Marketmodels::Products::Pathwise::CMarketModelPathwiseMultiDeflatedCaplet::Reset::get ()
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
    	(*_ppMarketModelPathwiseMultiDeflatedCaplet)->reset ( );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::IVector<UInt64>^ Cephei::QL::Models::Marketmodels::Products::Pathwise::CMarketModelPathwiseMultiDeflatedCaplet::SuggestedNumeraires::get ()
{
    try
    {
    	std::vector<QuantLib::Size> _rv = (std::vector<QuantLib::Size>)(*_ppMarketModelPathwiseMultiDeflatedCaplet)->suggestedNumeraires ( );   
        Cephei::IVector<UInt64>^ _nrv = gcnew CoVector<UInt64> (gcnew CUInt64Vector (_rv));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Models::Marketmodels::Products::Pathwise::IMarketModelPathwiseMultiDeflatedCaplet^ Cephei::QL::Models::Marketmodels::Products::Pathwise::CMarketModelPathwiseMultiDeflatedCaplet_Factory::Create (Cephei::IVector<Double>^ rateTimes, Cephei::IVector<Double>^ accruals, Cephei::IVector<Double>^ paymentTimes, Double strike)
{
    return gcnew CMarketModelPathwiseMultiDeflatedCaplet ( rateTimes,  accruals,  paymentTimes,  strike);
}
